Skip to content

ENH: __verify_trigger method and tests implementation#956

Open
juliomachad0 wants to merge 4 commits intoenh/eventsfrom
enh/events-verigy-triggers-and-actions
Open

ENH: __verify_trigger method and tests implementation#956
juliomachad0 wants to merge 4 commits intoenh/eventsfrom
enh/events-verigy-triggers-and-actions

Conversation

@juliomachad0
Copy link
Copy Markdown
Member

@juliomachad0 juliomachad0 commented Apr 28, 2026

Pull request type

  • Code changes (bugfix, features)

Checklist

  • Tests for the changes have been added (if needed)
  • Lint (black rocketpy/ tests/) has passed locally
  • All tests (pytest tests -m slow --runslow) have passed locally

Current behavior

Verify triggers function not implemented yet.
# TODO: check_trigger does note receive enough arguments to substitute parachute events
# TODO1: implement inspection of trigger function to verify:
# 1. It accepts **kwargs (accepts arbitrary keyword arguments)
# 2. Return type annotation is bool or can be tested to return bool
# 3. Consider allowing signature to be flexible (accepts **kwargs)
# to accommodate user-defined custom event_context keys
# TODO 2: implement inspection of action function to verify:
# 1. It accepts **kwargs (accepts arbitrary keyword arguments)
# 2. It can optionally return None or a dict with any of these keys:
# - Any custom keys to update event_context (dict type)
# 3. Raise ValueError if signature doesn't match expectations

New behavior

The verify_triggers method was implemented, trying to follow the instructions presented in the TODO. Also, an adaptation of the out_of_rail_trigger method in order to pass in the tests (following the defined standards)

Breaking change

  • No

juliomachad0 and others added 2 commits April 24, 2026 01:11
…out_of_rail_trigger to pass in __verify_trigger

Co-authored-by: Copilot <[email protected]>
@juliomachad0 juliomachad0 requested a review from a team as a code owner April 28, 2026 22:48
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 28, 2026

Codecov Report

❌ Patch coverage is 95.65217% with 1 line in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (enh/events@a927248). Learn more about missing BASE report.

Files with missing lines Patch % Lines
rocketpy/simulation/events.py 93.33% 1 Missing ⚠️
Additional details and impacted files
@@              Coverage Diff              @@
##             enh/events     #956   +/-   ##
=============================================
  Coverage              ?   81.16%           
=============================================
  Files                 ?      108           
  Lines                 ?    13920           
  Branches              ?        0           
=============================================
  Hits                  ?    11298           
  Misses                ?     2622           
  Partials              ?        0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

…on of the verify action method and adaptation of the out of rail trigger and action functions in flight class in order to pass in the new verifications

Co-authored-by: Copilot <[email protected]>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Implements signature/type verification for Event trigger/action callables in rocketpy/simulation, adds initial unit tests for trigger verification, and adapts Flight’s out-of-rail event trigger/action to comply with the new trigger interface.

Changes:

  • Added Event.__verify_trigger / Event.__verify_action callable inspection (signature + return annotation checks).
  • Updated Flight’s out-of-rail trigger/action wiring to pass event data via keyword arguments.
  • Added unit tests validating trigger signature/annotation validation.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
rocketpy/simulation/events.py Adds trigger/action verification logic using inspect.signature and type hints.
rocketpy/simulation/flight.py Updates out-of-rail trigger to accept **kwargs and action invocation to use keyword args.
tests/unit/simulation/test_events.py Adds tests for trigger verification error cases and acceptance.

Comment on lines +166 to +176
# verify if the return type annotation is None or dict
# Since is not possible to know for sure if the user is actually returning None or a dict,
# we enforce None or dict annotation to motivate users to actually return None or dict.
return_annotation = get_type_hints(action).get("return", None)
if return_annotation is not None and return_annotation is not (
type(None) or dict
):
raise ValueError(
f"The Action function of the {self.name} event must return None or a dictionary and must be annotated with '-> None' or '-> dict' for type checking.\n"
f"def {action.__name__}(**kwargs) -> None or dict:"
)
Comment thread rocketpy/simulation/flight.py Outdated
Comment on lines +1051 to +1070
@@ -1062,6 +1065,9 @@ def __handle_out_of_rail_event(self, phase, phase_index, node_index):
bool
True to indicate the simulation should break.
"""
phase = kwargs.get("phase")
phase_index = kwargs.get("phase_index")
node_index = kwargs.get("node_index")
Comment on lines +6 to +14
def test_verify_trigger_accepts_only_kwargs():
def trigger(**kwargs) -> bool:
return True

def action(**kwargs):
return None

event = Event(trigger=trigger, action=action, name="test")
assert event.trigger is trigger
Comment thread rocketpy/simulation/events.py Outdated
Comment on lines +125 to +127
# verify if the trigger function accepts only **kwargs arguments
s = inspect.signature(trigger)
if any(p.kind != inspect.Parameter.VAR_KEYWORD for p in s.parameters.values()):
…mplementation of new tests, mostly for verify actions method.
@juliomachad0
Copy link
Copy Markdown
Member Author

@MateusStano I think is ready to merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants